const {
  GiftShopModel
} = require('../../../utils/giftshop-model.js')
const giftShopModel = new GiftShopModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    navigateCode: '',
    tabIndex: 0,
    sortBy: 0,
    sortOrder: 'desc',
    searchResult: [],
    pageIndex: 1,
    pageSize: 12,
    totalCount: 0
  },

  globalSearch() {
    wx.reportAnalytics('classifysearch_globalsearch');
    wx.navigateTo({
      url: '/pages/giftShop/globalSearch/index'
    })
  },
  goDetail(e) {
    const { item } = e.currentTarget.dataset
    wx.reportAnalytics('classifysearch_goodsdetail');
    wx.navigateTo({
      url: `/pages/giftShop/goodsDetail/index?shopId=${item.shopId}&skuId=${item.skuId}&channelGoodsId=${item.channelGoodsId}`
    })
  },

  getSearchResult() {
    const merchantId = wx.getStorageSync('merchantId')
    const { shopId } = wx.getStorageSync('shopInfo')
    return new Promise((resolve) => {
      giftShopModel.getSearchResult({
        merchantId,
        shopId,
        keyword: this.data.keyword,
        navigateCode: this.data.navigateCode,
        sortBy: this.data.sortBy,
        sortOrder: this.data.sortOrder,
        offset: (this.data.pageIndex - 1) * this.data.pageSize,
        limit: this.data.pageSize
      }).then(data => {
        data.data = (data.data || []).map(item => {
          item._integerPrice = ((item.goodsPriceList || []).filter(item => item.priceType === 1)[0] || {}).amount
          item._beanPrice = ((item.goodsPriceList || []).filter(item => item.priceType === 2)[0] || {}).amount
          return item
        })
        resolve(data)
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none'
        })
      })
    })
  },

  tabChange(e) {
    const { index } = e.currentTarget.dataset
    let sortBy = index
    let sortOrder = ''
    if(index==0){
      wx.reportAnalytics('classifysearch_comprehensive');
    }else if(index==1){
      wx.reportAnalytics('classifysearch_salesvolume');
    }else if(index==2){
      wx.reportAnalytics('classifysearch_integral');
    }else if(index==3){
      wx.reportAnalytics('classifysearch_ticket');
    }
    if (index === 2 || index === 3) {
      sortBy = 2
      if (sortBy === this.data.sortBy) {
        sortOrder = this.data.sortOrder === 'desc' ? 'asc' : 'desc'
      } else {
        sortOrder = 'asc'
      }
    } else {
      sortOrder = 'desc'
    }
    this.setData({
      sortBy,
      sortOrder,
      pageIndex: 1,
      tabIndex: index
    })
    this.getSearchResult().then(data => {
      this.setData({
        searchResult: data.data || [],
        totalCount: data.totalCount
      })
    })
  },

  scrollEnd() {
    if (this.data.searchResult.length < this.data.totalCount) {
      this.setData({
        pageIndex: this.data.pageIndex + 1
      })
      this.getSearchResult().then(data => {
        this.setData({
          searchResult: this.data.searchResult.concat(data.data || []),
          totalCount: data.totalCount
        })
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: options.name || ' '
    })
    this.setData({
      navigateCode: options.navigateCode || '',
      keyword: options.keyword || '',
      tabIndex: options.tabIndex ? +options.tabIndex : 0
    })
  },
  onShow() {
    if (this.data.tabIndex) {
      this.tabChange({
        currentTarget: {
          dataset: {
            index: +this.data.tabIndex
          }
        }
      })
    } else {
      this.getSearchResult().then(data => {
        this.setData({
          searchResult: data.data || [],
          totalCount: data.totalCount
        })
      })
    }
  }
})